#coding=utf8
import itchat
from loguru import logger
from flask import Flask, request, send_file
import os

app = Flask(__name__)


@app.route('/send-message')
def send_message():
    message = request.args.get('message')
    username = request.args.get('username')
    itchat.send(message, toUserName=username)
    return "Message sent successfully."

@app.route('/send-file', methods=['POST'])
def send_file_route():
    file_type = request.args.get('type')
    username = request.args.get('username')
    if file_type in ['picture', 'video', 'audio']:
        file = request.files[file_type]
        file_path = os.path.join(lib_dir, file.filename)
        file.save(file_path)
        itchat.send_file(file_path, toUserName=username)
        os.remove(file_path)
        return "File sent successfully."
    else:
        return "Invalid file type.", 400

@app.route('/log')
def log():
    msg = request.args.get("msg")
    nickname = request.args.get("nickname")
    msg_type = request.args.get("type")
    logger.info(f"type={msg_type} msg={msg} name={nickname}")
    return "0"

if __name__ == '__main__':
    itchat.auto_login(True)
    app.run()